package xyz.hujf.base.createThread;

import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author Hujf
 * @title: ThreadTest
 * @date 2021-04-2117:59
 * @description: 创建和运行
 *
 * 使用继承的好处在于方便传参，可以在子类里面添加成员变量  通过set方法进行参数传递
 */
public class ThreadTest {

    public static class MyThread extends Thread{

        private int num ;
        @Override
        public void run() {
            super.run();
        }
    }

    public static class MyThread2 implements Runnable{


        private int num ;

        public MyThread2(int num) {
            this.num = num;
        }

        @Override
        public void run() {

        }
    }

    public static class MyThread3 implements Callable<String>{

        @Override
        public String call() throws Exception {
            return "返回结果字符串";
        }
    }

    @Test
    public void test3(){
        //创建线程任务 ，需要线程实例驱动 可以返回结果
        FutureTask <String>future = new FutureTask(new MyThread3());
        new Thread(future).start();
        try {
            String s = future.get();
            System.out.println(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void test1(){
        MyThread2 myThread2 = new MyThread2(1);

        new Thread(myThread2).start();

    }
}
